package com.ruoyi.web.sso.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.sso.common.util.AssertUtil;
import com.ruoyi.web.sso.common.util.UuidUtil;
import com.ruoyi.web.sso.domain.DTO.AreaSettingDTO;
import com.ruoyi.web.sso.domain.vo.AreaVO;
import com.ruoyi.web.sso.mapper.AreaMapper;
import com.ruoyi.web.sso.mapper.GroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.sso.mapper.DictDataMapper;
import com.ruoyi.web.sso.domain.DictData;
import com.ruoyi.web.sso.service.IDictDataService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 字典数据Service业务层处理
 *
 * @author ruoyi
 * @date 2021-10-28
 */
@Service
public class DictDataServiceImpl implements IDictDataService {
    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 查询字典数据
     *
     * @param dictCode 字典数据主键
     * @return 字典数据
     */
    @Override
    public DictData selectDictDataByDictCode(String dictCode) {
        return dictDataMapper.selectDictDataByDictCode(dictCode);
    }

    /**
     * 查询字典数据列表
     *
     * @param dictData 字典数据
     * @return 字典数据
     */
    @Override
    public List<DictData> selectDictDataList(DictData dictData) {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 新增字典数据
     *
     * @param dictData 字典数据
     * @return 结果
     */
    @Override
    public int insertDictData(DictData dictData) {
        dictData.setDictCode(UuidUtil.getUuid());
        return dictDataMapper.insertDictData(dictData);
    }

    /**
     * 修改字典数据
     *
     * @param dictData 字典数据
     * @return 结果
     */
    @Override
    public int updateDictData(DictData dictData) {
        return dictDataMapper.updateDictData(dictData);
    }

    /**
     * 批量删除字典数据
     *
     * @param dictCodes 需要删除的字典数据主键
     * @return 结果
     */
    @Override
    public int deleteDictDataByDictCodes(String dictCodes) {
        return dictDataMapper.deleteDictDataByDictCodes(Convert.toStrArray(dictCodes));
    }

    /**
     * 删除字典数据信息
     *
     * @param dictCode 字典数据主键
     * @return 结果
     */
    @Override
    public int deleteDictDataByDictCode(String dictCode) {
        return dictDataMapper.deleteDictDataByDictCode(dictCode);
    }

    @Override
    public AreaSettingDTO selectKeyAndByAreaIdDictData(String label, String type, String areaId) {
        return dictDataMapper.selectKeyAndByAreaIdDictData(label, type, areaId);
    }

    @Override
    public List<AreaSettingDTO> getAllAreaSettings(String areaId, String prefix) {
        return dictDataMapper.getAllAreaSettings(areaId, prefix);
    }

    @Override
    @Transactional
    public Boolean addAreaSetting(String key, String value, String description, String extension, String areaId, String type) {
        DictData dictData = new DictData();
        dictData.setDictCode(UuidUtil.getUuid());
        dictData.setDictLabel(key);
        dictData.setDictValue(value);
        dictData.setRemark(description);
        dictData.setAreaId(areaId);
        dictData.setDictType(type);
        dictData.setExtension(extension);
        insertAndUpdateDictData(dictData);
        return true;
    }

    @Override
    @Transactional
    public Integer addAreaSettings(String keys, String values, String description, String extension, String areaId, String type) {
        String[] key = Convert.toStrArray(keys);
        String[] value = Convert.toStrArray(values);
        for (int i = 0; i < key.length; i++) {
            addAreaSetting(key[i], value[i], description, extension, areaId, type);
        }
        return 1;
    }

    @Override
    public int insertAndUpdateDictData(DictData dictData) {
        dictData.setDictCode(UuidUtil.getUuid());
        dictData.setUpdateTime(new Date());
        AreaSettingDTO data = dictDataMapper.selectAreaIdDictData(dictData.getDictLabel(), dictData.getDictType(), dictData.getAreaId(), dictData.getGroupId());
        if (AssertUtil.isNotEmpty(data)) {
            dictData.setDictCode(data.getId());
            return dictDataMapper.updateDictData(dictData);
        }
        return dictDataMapper.insertDictData(dictData);
    }

    @Override
    public boolean isData(DictData dictData) {
        AreaSettingDTO data = dictDataMapper.selectAreaIdDictData(dictData.getDictLabel(), dictData.getDictType(), dictData.getAreaId(), dictData.getGroupId());
        if (AssertUtil.isNotEmpty(data)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean isDataList(List<DictData> dictData) {
        for (int i = 0; i < dictData.size(); i++) {
            AreaSettingDTO data = dictDataMapper.selectAreaIdDictData(dictData.get(i).getDictLabel(), dictData.get(i).getDictType(), dictData.get(i).getAreaId(), dictData.get(i).getGroupId());
            if (AssertUtil.isNotEmpty(data)) {
                return false;
            }
        }
        return true;
    }
}
